const { dateFormat } = require('@/utils/common.util')
const db = require('@/db/db-connection')

class SignInModel {
  async add (userId) {
    const signTime = dateFormat(new Date(), 'YYYY-mm-dd')

    const sql = 'INSERT INTO sign_in (userId, signTime) VALUES(?, ?)'

    try {
      await db.query(sql, [userId, signTime])
    } catch (error) {
      throw new Error(error)
    }
  }

  async exist (userId, signTime) {
    const sql = 'SELECT userId FROM sign_in WHERE userId = ? AND signTime = ?'

    try {
      const data = await db.query(sql, [userId, signTime])

      return data[0]
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new SignInModel()
